package com.secneo.appshare.adapter;

import java.util.List;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.secneo.appshare.R;
import com.secneo.appshare.activities.AppsOtherUserActivity;
import com.secneo.appshare.activities.ReplayPrivateMsgActivity;
import com.secneo.appshare.api.LocalStore;
import com.secneo.appshare.common.Constants;
import com.secneo.appshare.privatemsg.PrivateMsg;

public class PrivateMsgListAdapter extends BaseAdapter {
	private List<PrivateMsg> mList;
	private Context mContext;
	private Handler mHandler;
	private boolean mIsByUid ;

	public PrivateMsgListAdapter(Context ctx, List<PrivateMsg> list, 
			Handler handler , boolean isByUid) {
		this.mContext = ctx;
		this.mList = list;
		this.mHandler = handler ;
		this.mIsByUid = isByUid ;
	}
	
	public void freeDrawable()
	{
		for(int i=0; i<mList.size(); i++)
		{
			if(!mList.get(i).getO_iconurl().contains("buildin") &&
					mList.get(i).o_icon != null){
				BitmapDrawable a = (BitmapDrawable) mList.get(i).o_icon;
				if (a != null && !a.getBitmap().isRecycled())
					a.getBitmap().recycle();
				mList.get(i).o_icon = null;
			}
		}
		//mList.clear();
		System.gc();
	}

	public void setIsByUid(boolean isByUid){
		mIsByUid = isByUid ;
	}
	
	public void setList(List<PrivateMsg> list){
		mList = list ;
	}
	
	@Override
	public int getCount() {
		return mList.size();
	}

	@Override
	public Object getItem(int position) {
		return mList.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		final int pp = position ;
		PrivateMsgItem privateMsgItem;
		final PrivateMsg privateMsg = mList.get(position);
		if (convertView == null) {
			View v = LayoutInflater.from(mContext).inflate(
					R.layout.appshare_activity_item, null);

			privateMsgItem = new PrivateMsgItem();
			privateMsgItem.mDataBackground = (LinearLayout) v
					.findViewById(R.id.data_background);
			privateMsgItem.mUserIcon = (ImageView) v
					.findViewById(R.id.activity_user_icon);
			privateMsgItem.mUserName = (TextView) v
					.findViewById(R.id.activity_user_name);
			privateMsgItem.mData = (TextView) v
					.findViewById(R.id.activity_details);
			privateMsgItem.mData.setPadding(18, 0, 0, 0);
			privateMsgItem.mDate = (TextView) v
					.findViewById(R.id.activity_date);
			privateMsgItem.mIsNewIcon = (ImageView) v
					.findViewById(R.id.activity_app_icon) ;
			v.setTag(privateMsgItem);
			convertView = v;
		} else {
			privateMsgItem = (PrivateMsgItem) convertView.getTag();
		}

		if (privateMsg.getO_icon() == null)
			privateMsgItem.mUserIcon.setImageResource(R.drawable.some_app);
		else
			privateMsgItem.mUserIcon.setImageDrawable(privateMsg.getO_icon());
		
		if(privateMsg.isNew())
		{
			privateMsgItem.mIsNewIcon.setImageResource(R.drawable.new_msg) ;
			privateMsgItem.mIsNewIcon.setVisibility(View.VISIBLE);
		}
		else
			privateMsgItem.mIsNewIcon.setVisibility(View.INVISIBLE);

		privateMsgItem.mUserName.setText(privateMsg.getO_username());

		privateMsgItem.mData.setText(privateMsg.getData());

		// String time = privateMsg.getTime().toString() ;
		privateMsgItem.mDate
				.setText(privateMsg.getTime());

		Drawable background = mContext.getResources().getDrawable(
				R.drawable.privatemsg_data_back);

		privateMsgItem.mDataBackground.setBackgroundDrawable(background);

		privateMsgItem.mUserIcon.setOnClickListener(new OnClickListener() {
			// 点击用户头像后
			@Override
			public void onClick(View v) {
				Intent intent = new Intent();
				intent.setClass(mContext, AppsOtherUserActivity.class);

				Bundle mBundle = new Bundle();
				String uid = LocalStore.getLocaleStoreInstance(mContext).getUid();
				mBundle.putString("uid", uid);
				mBundle.putString("other_uid", privateMsg.getFid());
				mBundle.putString("user_name", privateMsg.getO_username());
				mBundle.putString("user_icon", privateMsg.getO_iconurl());
				intent.putExtras(mBundle);
				mContext.startActivity(intent);
			}
		});

		privateMsgItem.mDataBackground
				.setOnClickListener(new OnClickListener() {
					// 点击listitem
					@Override
					public void onClick(View v) {
						//显示dialog
						if(mIsByUid){
							getDialog(privateMsg , pp).show() ;
						} else {
							Message msg = new Message() ;
							msg.what = Constants.LOOK_PRIVATE_MSG_BY_UID ;
							Bundle data = new Bundle() ;
							data.putString(Constants.UID_ACTION, privateMsg.getFid()) ;
							msg.setData(data) ;
							mHandler.sendMessage(msg) ;
						}
					}

				});

		return convertView;
	}
	
	private void replayMsg(final PrivateMsg privateMsg){
		Intent intent = new Intent() ;
		Bundle bundle = new Bundle() ;
		bundle.putString(Constants.UID_ACTION, privateMsg.getFid()) ;
		intent.putExtras(bundle) ;
		intent.setClass(mContext, ReplayPrivateMsgActivity.class) ;
		mContext.startActivity(intent) ;
	}
	
	private void deleteMsg(final PrivateMsg privateMsg , int position){
		Message msg1 = new Message() ;
		msg1.what = Constants.DELETE_PRIVATE_MSG_ACTION ;
		Bundle data1 = new Bundle() ;
		data1.putInt(Constants.UID_ACTION, privateMsg.getMsg_id()) ;
		data1.putInt("position", position) ;
		msg1.setData(data1) ;
		mHandler.sendMessage(msg1) ;
	}
	
	private Dialog getDialog(final PrivateMsg privateMsg , final int position){
		if(!mIsByUid)
			return new AlertDialog.Builder(mContext).setTitle(
					R.string.select_function).setItems(
					R.array.select_private_msg_function_array,
					new DialogInterface.OnClickListener() {
						public void onClick(DialogInterface dialog,
								int which) {
							switch(which){
							case 0://回复私信
								replayMsg(privateMsg) ;
								break ;
							case 1://查看私信对话
								Message msg = new Message() ;
								msg.what = Constants.LOOK_PRIVATE_MSG_BY_UID ;
								Bundle data = new Bundle() ;
								data.putString(Constants.UID_ACTION, privateMsg.getFid()) ;
								msg.setData(data) ;
								mHandler.sendMessage(msg) ;
								break ;
							case 2://删除
								deleteMsg(privateMsg , position) ;
								break;
							default:
								break;
							}
						}
					}).create() ;
		else
			return new AlertDialog.Builder(mContext).setTitle(
					R.string.select_function).setItems(
					R.array.select_private_msg1_function_array,
					new DialogInterface.OnClickListener() {
						public void onClick(DialogInterface dialog,
								int which) {
							switch(which){
							case 0://回复私信
								replayMsg(privateMsg) ;
								break ;
							case 1://删除
								deleteMsg(privateMsg , position) ;
								break;
							default:
								break;
							}
						}
					}).create() ;
	}
	
	class PrivateMsgItem {
		LinearLayout mDataBackground;
		ImageView mUserIcon;
		TextView mUserName;
		TextView mData;
		ImageView mIsNewIcon ;
		TextView mDate;
	}
}
